﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using SelfService.Model;
using SelfService.Model.Interface;
using SelfService.Model.Models;
using WalkingTec.Mvvm.Core;


namespace SelfService.Tasks
{
    [Display(Name = "查询支付结果")]
    public class PayTask : BaseTask
    {
        private readonly IServiceProvider _provider;
        public PayTask(WTMContext context, IServiceProvider provider) : base(context)
        {
            _provider = provider;
        }

        public override async Task<bool> Process()
        {
            this.Logger("查询支付结果", ActionLogTypesEnum.Normal);
            try
            {
                //调用支付接口
                IPaySystem service;
                try
                {
                    var scope = _provider.CreateScope();
                    service = scope.ServiceProvider.GetService<IPaySystem>();
                }
                catch (Exception)
                {
                    this.Logger($"没有启用支付服务", ActionLogTypesEnum.Exception);
                    return false;
                }
                if (service == null)
                {
                    this.Logger($"没有启用支付服务", ActionLogTypesEnum.Exception);
                    return false;
                }
                var dc = Context.DC;
                var list = await dc.Set<SysPay>()
                    .Where(e => e.PayStatus == PayStatusEnum.支付中)
                    .ToListAsync();
                foreach (var element in list)
                {
                    var result = await service.GetPayResult(element);
                    if (result.Code == 200)
                    {
                        element.PayStatus = result.Data.PayStatus;
                        element.PayTime = result.Data.PayTime;
                        element.UpdateTime = DateTime.Now;
                        element.SerialsNumber = result.Data.SerialsNumber;
                        this.Logger($"订单[{element.OutOrderNo}]支付成功", ActionLogTypesEnum.Normal);
                    }
                    else if (result.Code == 3)
                    {
                        element.PayStatus = PayStatusEnum.支付失败;
                        element.UpdateTime = DateTime.Now;
                        this.Logger($"订单[{element.OutOrderNo}]支付失败", ActionLogTypesEnum.Normal);
                    }
                    else if (result.Code == 2)
                    {
                        if (element.UpdateTime < DateTime.Now.AddMinutes(-3))
                        {
                            element.PayStatus = PayStatusEnum.支付失败;
                            element.UpdateTime = DateTime.Now;
                            element.UseMessage = "订单超时未支付, 系统自动关闭";
                            this.Logger($"订单[{element.OutOrderNo}]超时未支付, 系统自动关闭", ActionLogTypesEnum.Normal);
                        }
                    }
                    else
                    {
                        this.Logger($"查询支付结果出错, 请求数据:{result.Request}, 返回数据:{result.Response}", ActionLogTypesEnum.Exception);
                    }
                }
                await dc.SaveChangesAsync();
                return true;
            }
            catch (Exception e)
            {
                this.Logger($"查询支付结果异常{e.Message}, 详细信息:{JsonConvert.SerializeObject(e)}", ActionLogTypesEnum.Exception);
            }
            return false;
        }
    }
}
